package com.dcy.system.filter.config;

import com.dcy.system.filter.web.filter.LogServletFilter;
import com.dcy.system.filter.web.filter.LogWebInvokeTimeFilter;
import com.yomahub.tlog.springboot.TLogWebAutoConfiguration;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

/**
 * 日志配置
 *
 * @author YuChangjiang
 */
@Configuration
@EnableAutoConfiguration(exclude = {TLogWebAutoConfiguration.class})
public class LogConfig {

    /**
     * 配置 TLog 在 Servlet 中的生成
     */
    @Bean
    public FilterRegistrationBean<LogServletFilter> buildTLogServletFilter() {
        FilterRegistrationBean<LogServletFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new LogServletFilter());
        registration.addUrlPatterns("/*");
        registration.setName("logServletFilter");
        registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return registration;
    }

    /**
     * 配置全局访问性能拦截
     */
    @Bean
    public FilterRegistrationBean<LogWebInvokeTimeFilter> buildLogWebInvokeTimeFilter() {
        FilterRegistrationBean<LogWebInvokeTimeFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new LogWebInvokeTimeFilter());
        registration.addUrlPatterns("/*");
        registration.setName("logWebInvokeTimeFilter");
        registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return registration;
    }

}
